<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="单位名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="单位名称"></el-input>
    </el-form-item>
    <el-form-item label="主任姓名" prop="officeChiefName">
      <el-input v-model="dataForm.officeChiefName" placeholder="办公室主任名称"></el-input>
    </el-form-item>
    <el-form-item label="主任电话" prop="officeChiefNum">
      <el-input v-model="dataForm.officeChiefNum" placeholder="办公室主任电话"></el-input>
    </el-form-item>
    <el-form-item label="会员总数" prop="totalMember" hidden>
      <el-input v-model="dataForm.totalMember" placeholder="会员总数"></el-input>
    </el-form-item>
    <el-form-item label="" prop="def0" hidden>
      <el-input v-model="dataForm.def0" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def1" hidden>
      <el-input v-model="dataForm.def1" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def2" hidden>
      <el-input v-model="dataForm.def2" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isLoading: false,
        dataForm: {
          name: '',
          officeChiefName: '',
          officeChiefNum: '',
          qrCodeUrl: '',
          totalMember: '0',
          def0: '',
          def1: '',
          def2: '',
          def3: '',
          def4: ''
        },
        dataRule: {
          name: [
            { required: true, message: '单位名称不能为空', trigger: 'blur' }
          ],
          officeChiefName: [
            { required: true, message: '办公室主任名称不能为空', trigger: 'blur' }
          ],
          officeChiefNum: [
            { required: true, message: '办公室主任电话不能为空', trigger: 'blur' }
          ],
          qrCodeUrl: [
            { required: false, message: '二维码保存地址不能为空', trigger: 'blur' }
          ],
          totalMember: [
            { required: false, message: '会员总数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/unit/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.unit.name
                this.dataForm.officeChiefName = data.unit.officeChiefName
                this.dataForm.officeChiefNum = data.unit.officeChiefNum
                this.dataForm.qrCodeUrl = data.unit.qrCodeUrl
                this.dataForm.totalMember = data.unit.totalMember
                this.dataForm.def0 = data.unit.def0
                this.dataForm.def1 = data.unit.def1
                this.dataForm.def2 = data.unit.def2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/unit/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'officeChiefName': this.dataForm.officeChiefName,
                'officeChiefNum': this.dataForm.officeChiefNum,
                'qrCodeUrl': this.dataForm.qrCodeUrl,
                'totalMember': this.dataForm.totalMember,
                'isDel': 0,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1,
                'def2': this.dataForm.def2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.isLoadingVisible()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      }
    }
  }
</script>
